﻿using Mengsk.ProxyDelivery.Converters;
using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Print.Service;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Media;

namespace Mengsk.ProxyDelivery.Print.ViewModels
{
    class PrintOrderViewModel : DependencyObject, IComparable<PrintOrderViewModel>
    {
        public static VendorDoorConverter doorConverter = new VendorDoorConverter();

        public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(PrintOrderViewModel));
        public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(Brush), typeof(PrintOrderViewModel));
        public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(string), typeof(PrintOrderViewModel));
        public static readonly DependencyProperty DeliveryCompanyProperty = DependencyProperty.Register("DeliveryCompany", typeof(string), typeof(PrintOrderViewModel));
        public static readonly DependencyProperty DeliveryNumberProperty = DependencyProperty.Register("DeliveryNumber", typeof(string), typeof(PrintOrderViewModel));
        public bool IsChecked { get { return (bool)this.GetValue(IsCheckedProperty); } set { this.SetValue(IsCheckedProperty, value); } }

        public Brush Background { get { return (Brush)this.GetValue(BackgroundProperty); } set { this.SetValue(BackgroundProperty, value); } }

        public string State { get { return (string)this.GetValue(StateProperty); } set { this.SetValue(StateProperty, value); } }

        public string DeliveryCompany { get { return (string)this.GetValue(DeliveryCompanyProperty); } set { this.SetValue(DeliveryCompanyProperty, value); } }

        public string DeliveryNumber { get { return (string)this.GetValue(DeliveryNumberProperty); } set { this.SetValue(DeliveryNumberProperty, value); } }

        public string WuliuId { get; set; }

        public string SenderName { get; set; }

        public string SenderPhone { get; set; }

        public Order Source { get; private set; }

        public string Action { get { return "删除"; } }

        public string Goods { get; private set; }

        public string DoorNumber { get; private set; }


        public string Gifts { get; set; }

        private void UpdateGoodsInfo()
        {
            StringBuilder sb = new StringBuilder();
            if (this.Source.OrderGoodss != null)
                foreach (var goods in this.Source.OrderGoodss.Where(obj => obj.State != OrderState.Closed && obj.State != OrderState.Cancled && obj.State != OrderState.Notsale && obj.State != OrderState.Spilted))
                {
                    sb.Append(VendorService.FormatVendorName(goods.Vendor) + " " + goods.Number + " " + goods.Edtion + " " + goods.Color + " " + goods.Size + " " + goods.Count + ", ");
                }

            this.Goods = sb.ToString();
        }

        private void UpdateDoorNumber()
        {
            string sb = "";
            if (this.Source.OrderGoodss != null)
            {
                foreach (var goods in this.Source.OrderGoodss.Where(obj => obj.State != OrderState.Closed && obj.State != OrderState.Cancled && obj.State != OrderState.Notsale && obj.State != OrderState.Spilted))
                {
                    string door = VendorServiceWithCatch.FormatVendorAreaAndDoor(goods.Vendor);
                    if (sb.Contains(door) == false)
                    {
                        sb += door + " ";
                    }
                }
            }

            this.DoorNumber = sb;
        }

        private void UpdateGifts()
        {
            StringBuilder sb = new StringBuilder();
            if (this.Source.OrderGifts != null)
                foreach (var gift in this.Source.OrderGifts)
                {
                    sb.Append(gift.GiftName + " " + gift.Count + ", ");
                }
            this.Gifts = sb.ToString();
        }

        public PrintOrderViewModel(Order order)
        {
            if (order == null)
            {
                throw new ArgumentNullException("order");
            }

            this.Source = order;
            this.Background = null;
            this.State = "";
            this.DeliveryCompany = order.DeliveryCompany;
            this.DeliveryNumber = order.DeliveryNumber;
            this.IsChecked = true;
            var senderInfo = SellerServiceInfoWithCatch.GetSellerSenderInfo(order.SellerId);

            if (senderInfo != null)
            {
                this.SenderName = senderInfo.SenderName;
                this.SenderPhone = senderInfo.SenderPhone;
            }
            else
            {
                this.SenderPhone = "";
                this.SenderName = "";
            }

            this.UpdateDoorNumber();
            this.UpdateGifts();
            this.UpdateGoodsInfo();
        }

        public int CompareTo(PrintOrderViewModel other)
        {
            if (other == null)
            {
                return 1;
            }

            if (this.DoorNumber.Equals(other.DoorNumber) == false)
            {
                return this.DoorNumber.CompareTo(other.DoorNumber);
            }

            return this.Goods.CompareTo(other.Goods);
        }
    }
}
